#!/bin/sh
#Usage: directories need to be named with phylum names, such as cyano or chlorobi
#Eg: dissertation_SingleCopyGenes.sh genes.list cyano
#    dissertation_RibosomalGenes.sh r53.list cyano

genes=$1
phylum=$2
count=`wc -l $genes | awk '{print $1}'`

#Doing BLAST
for i in `cat $genes`;do
    echo "Working on $i BLAST for $phylum"
    cd /host/Users/JS/UH-work/gloeobacter/final_work/comparisons/orthologs/$phylum
    for j in `cat $phylum.list`;do
        blastall -p blastp -i /host/Users/JS/UH-work/gloeobacter/final_work/comparisons/ribosomal_genes/$i.fasta -d $j.refseq.faa -F F -e 0.01 -m 8 -o $i.$j.blastp
        head -1 $i.$j.blastp | awk '{print $2}' > tmp.out
        exfasta `cat tmp.out` $j.refseq.faa > $i.$j.fasta
        cat $i.$j.fasta >> $j.$count.fasta
        echo ">rc$j" > dummy.tmp
        cp $j.$count.fasta $j."r"$count"c".fasta
        sed -i 's/>.*//g' $j."r"$count"c".fasta
        cat dummy.tmp $j."r"$count"c".fasta > $j.tmp
        bp_seqconvert.pl --from fasta --to fasta < $j.tmp > $j."r"$count"c".fasta
        rm -f dummy.tmp $j.tmp tmp.out *.blastp
        #echo "Done with $i from $j"
        #cat $j."r"$count"c".fasta >> $phylum."rc"$count.fasta
    done
    echo "Done with $i for $phylum"
done
cat *$count"c".fasta > $phylum."rc"$count.fasta
mv $phylum."rc"$count.fasta /host/Users/JS/UH-work/gloeobacter/final_work/comparisons/ribosomal_genes/
